<?php
class Admin_user_model extends CI_Model
{
    private $table   = 'admin_user';
    private $table_2 = 'role';

    public function total($params=array()) 
    {
        if (!empty($params['name'])) {
            $this->db->where("((`id`='{$params['name']}') OR (`name` LIKE '%{$params['name']}%'))");
        }
        if (!empty($params['realname'])) {
            $this->db->like('realname', $params['realname']);
        }
        if (!empty($params['email'])) {
            $this->db->where('email', $params['email']);
        }
        if (!empty($params['role_id'])) {
            $this->db->where('role_id', $params['role_id']);
        }
        if (isset($params['flag']) && is_numeric($params['flag'])) {
            $this->db->where('flag', $params['flag']);
        }
        return $this->db->count_all_results($this->table);
    }

    public function page_list($page_num, $num, $params=array())
    {
        if (!empty($params['name'])) {
            $this->db->where("((`id`='{$params['name']}') OR (`name` LIKE '%{$params['name']}%'))");
        }
        if (!empty($params['realname'])) {
            $this->db->like('realname', $params['realname']);
        }
        if (!empty($params['email'])) {
            $this->db->where('email', $params['email']);
        }
        if (!empty($params['role_id'])) {
            $this->db->where('role_id', $params['role_id']);
        }
        if (isset($params['flag']) && is_numeric($params['flag'])) {
            $this->db->where('flag', $params['flag']);
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }

    public function insertAdminuser($postData)
    {
        $data = array(
            'name'       => $postData['name'],
            'email'      => trim($postData['email']),
            'password'   => sha1(base64_encode(md5($postData['password']))),
            'role_id'    => trim($postData['role_id']),
            'created_at' => date('Y-m-d H:i:s', time())
        );
        if (!empty($postData['realname'])) {
            $data['realname'] = trim($postData['realname']);
        }
        
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateAdminuser($postData)
    {
        $data = array(
            'email'      => trim($postData['email']),
            'updated_at' => date('Y-m-d H:i:s', time())
        );
        if (!empty($postData['name'])) {
            $data['name'] = trim($postData['name']);
        }
        if (!empty($postData['role_id'])) {
            $data['role_id'] = trim($postData['role_id']);
        }
        if (!empty($postData['realname'])) {
            $data['realname'] = trim($postData['realname']);
        }
        if (!empty($postData['modify_password'])) {
            $data['password'] = sha1(base64_encode(md5($postData['password'])));
        }
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }
    
    public function deleteById($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }
    
    public function findById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }
    
    public function validateAdminuser($postData)
    {
        $this->db->where('name', $postData['name']);
        $this->db->or_where('email', $postData['email']);
        if ($this->db->count_all_results($this->table) > 0) {
            return true;
        }
        return false;
    }
    
    public function login($postData)
    {
        $this->db->select('id, name, realname, email, role_id, created_at, flag');
        $this->db->from($this->table.' as admin_user');
        $this->db->where('admin_user.name', $postData['username']);
        $this->db->where('admin_user.password', sha1(base64_encode(md5($postData['password']))));
        return $this->db->get();
    }
    
    function resetpwd($uid)
    {
        $this->db->where('id', $uid);
        $data = array('password'=>sha1(base64_encode(md5('123456'))));
        return $this->db->update($this->table, $data);
    }
}